package com.chen.wow;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

public class DateUtil {
	public static void main(String[] args){
//		try {
//			long result = stringToLong("20160602151259","yyyymmddhhmmss");
//			System.out.println("result=" + result);
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		LocalDate date = LocalDate.now();//不可变的，线程安全的
		System.out.println(date);
	}

	public static Date stringToDate(String strTime, String formatType)
 			throws ParseException {
 		SimpleDateFormat formatter = new SimpleDateFormat(formatType);
 		Date date = null;
 		date = formatter.parse(strTime);
 		return date;
 	}
	
	public static long stringToLong(String strTime, String formatType)
 			throws ParseException {
 		Date date = stringToDate(strTime, formatType); // String类型转成date类型
 		if (date == null) {
 			return 0;
 		} else {
 			long currentTime = dateToLong(date); // date类型转成long类型
 			return currentTime;
 		}
 	}
	
	public static long dateToLong(Date date) {
 		return date.getTime();
 	}
	
	public static long dateToLong(Date date, String formatType) throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(formatType);
 		return formatter.parse(formatter.format(date)).getTime();
 	}
}
